/**
 * 
 */
package com.and.xmppsdk;

import org.drl.xmpp.TalkClient;
import org.drl.xmpp.UserInfo;
import org.drl.xmpp.impl.TalkLoginStateListener;
import org.drl.xmpp.util.StringHelper;

import com.and.xmppsdk.util.ToastHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * @author hljdr@gmail.com
 * xmppsdktest@jabber.org
 * andxmppsdk@jabber.org;
 */
public class LoginActivity extends Activity implements OnClickListener ,TalkLoginStateListener {

	Button btn_login;
	Button btn_exit;
	TextView tv_status;
	EditText et_username;
	EditText et_passwd;
	EditText et_host;
	EditText et_port;
	
	TalkClient talk;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.aty_login);
		btn_exit = (Button) findViewById(R.id.btn_exit);
		btn_login = (Button) findViewById(R.id.btn_login);
		tv_status = (TextView) findViewById(R.id.tv_stauts);
		et_username = (EditText) findViewById(R.id.et_user);
		et_passwd = (EditText) findViewById(R.id.et_passwd);
		et_host = (EditText) findViewById(R.id.et_host);
		et_port = (EditText) findViewById(R.id.et_port);
		//
		
		talk = XmppApplication.getXmppSDK(this);
		if(talk.isAuthentificated()){
			tv_status.setText("已登录");
			ToastHelper.showTet(this, "账户已登录...");
			goHome();
		}
		TextView title = (TextView) findViewById(R.id.actionbar_title);
		
		title.setText("登录");
		btn_login.requestFocus();
	}
	private void goHome()
	{
		Intent mIm = new Intent(this,HomeActivity.class);
		startActivity(mIm);
		finish();
	}
	@Override
	protected void onResume() {
		super.onResume();
		if(talk!=null){
			talk.addTalkLoginStateListener(this);
		}
	}
	@Override
	protected void onStop() {
		super.onStop();
		if(talk!=null){
			talk.removeTalkLoginStateListener(this);
		}
	}
	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btn_exit) {
			finish();
		} else if (v.getId() == R.id.btn_login) {
			setupLogin();
		}
	}

	private void setupLogin() {
		String _user = et_username.getText().toString();
		String _pw = et_passwd.getText().toString();
		String _host = et_host.getText().toString();
		String _prot = et_port.getText().toString();

		if (!StringHelper.isText(_user)) {
			et_username.setError("请输入用户名！");
			et_username.requestFocus();
			return ;
		}
		if (!StringHelper.isText(_pw)) {
			et_passwd.setError("请输入密码");
			et_passwd.requestFocus();
			return ;
		}
		if (!StringHelper.isText(_host)) {
			et_host.setError("请输入主机");
			et_host.requestFocus();
			return ;
		}
		if (!StringHelper.isText(_prot)) {
			et_port.setError("请输入端口号");
			et_port.requestFocus();
			return ;
		}
		//
		UserInfo user = new UserInfo();
		user.setUserName(_user);
		user.setPassword(_pw);
		talk.setHost(_host, Integer.parseInt(_prot));
		talk.setUserInfo(user);
		talk.connectAsync();
	}
	@Override
	public void OnChageStatus(int state,String text) {
		tv_status.setText(text);
		if(state==TalkLoginState.LOGIN_SUCC){
			goHome();
			finish();
		}
	}
}
